<template>
  <div>
    <div style="display: flex; justify-content: center; margin-top: 10px">
      <el-input
        placeholder="搜索用户..."
        prefix-icon="el-icon-search"
        v-model="keyWords"
        @keydown.enter.native="doSearch"
        style="width: 400px; margin-right: 10px"
      ></el-input>
      <el-button type="primary" @click="doSearch" icon="el-icon-search">搜索</el-button>
    </div>
    <div class="admin-container">
      <el-card class="admin-card" v-for="(admin, index) in admins" :key="index">
        <div slot="header" class="clearfix">
          <span>{{ admin.name }}</span>
          <el-button
            style="float: right; padding: 3px 0; color: red"
            type="text"
            icon="el-icon-delete"
            @click="deleteAdmin(admin)"
          ></el-button>
        </div>
        <div>
          <div class="img-container">
            <img :src="admin.userFace" alt="admin.name" :title="admin.name" class="userface-img" />
          </div>
        </div>
        <div class="userinfo-container">
          <div>用户名：{{ admin.name }}</div>
          <div>手机号码：{{ admin.phone }}</div>
          <div>电话号码：{{ admin.telephone }}</div>
          <div>地址：{{ admin.address }}</div>
          <div>
            用户状态：
            <el-switch
              v-model="admin.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              inactive-text="禁用"
              @change="enabledChange(admin)"
            ></el-switch>
          </div>
          <div>
            用户角色：
            <el-tag
              style="margin-right: 4px"
              type="success"
              v-for="(role, index) in admin.roles"
              :key="index"
            >{{ role.nameZh }}</el-tag>
            <el-popover
              placement="bottom"
              title="角色列表"
              width="200"
              @show="showPop(admin)"
              @hide="hidePop(admin)"
              trigger="click"
            >
              <el-select v-model="selectedRoles" multiple placeholder="请选择">
                <el-option
                  v-for="(role, index) in allRoles"
                  :key="index"
                  :label="role.nameZh"
                  :value="role.id"
                ></el-option>
              </el-select>
              <el-button slot="reference" type="text" icon="el-icon-more"></el-button>
            </el-popover>
          </div>
          <div>备注：{{ admin.remark }}</div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      admins: [],
      keyWords: '',
      allRoles: '',
      selectedRoles: []
    }
  },
  mounted () {
    this.initAdmins()
  },
  methods: {
    hidePop (admin) {
      const roles = []
      Object.assign(roles, admin.roles)
      let flag = false
      if (roles.length !== this.selectedRoles.length) {
        flag = true
      } else {
        for (let i = 0; i < roles.length; i++) {
          const role = roles[i]
          for (let j = 0; j < this.selectedRoles.length; j++) {
            const sr = this.selectedRoles[j]
            if (role.id === sr) {
              roles.splice(i, 1)
              i--
              break
            }
          }
        }
        if (roles.length !== 0) {
          flag = true
        }
      }

      if (flag) {
        let url = '/system/admin/role?adminId=' + admin.id
        this.selectedRoles.forEach((sId) => {
          url += '&rids=' + sId
        })
        this.putRequest(url).then((resp) => {
          if (resp) {
            this.initAdmins()
          }
        })
      }
    },
    showPop (admin) {
      this.initAllRoles()
      const roles = admin.roles
      this.selectedRoles = []
      roles.forEach((e) => {
        this.selectedRoles.push(e.id)
      })
    },
    initAllRoles () {
      this.getRequest('/system/admin/roles')
        .then((result) => {
          if (result) {
            this.allRoles = result
          }
        })
        .catch((err) => { this.$message.error(err) })
    },
    enabledChange (admin) {
      this.putRequest('/system/admin/', admin).then((resp) => {
        if (resp) {
          this.initAdmins()
        }
      })
    },
    deleteAdmin (admin) {
      this.$confirm(
        '此操作将永久删除[' + admin.name + ']角色, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          this.deleteRequest('/system/admin/' + admin.id).then(
            (resp) => {
              if (resp) {
                this.initAdmins()
              }
            }
          )
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    doSearch () {
      this.initAdmins()
    },
    initAdmins () {
      this.getRequest('/system/admin/?keywords=' + this.keyWords)
        .then((result) => {
          if (result) {
            this.admins = result
          }
        })
        .catch((err) => { this.$message.error(err) })
    }
  }
}
</script>

<style scoped>
.userinfo-container {
  font-size: 12px;
  color: #409eff;
}
.img-container {
  width: 100%;
  display: flex;
  justify-content: center;
}
.userface-img {
  width: 72px;
  height: 72px;
  border-radius: 72px;
}
.admin-container {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  margin-top: 10px;
}
.admin-card {
  width: 350px;
  margin-bottom: 10px;
}
</style>
